package service

import (
	"github.com/gin-gonic/gin"
	"monment/basic/config"
	"monment/handler/model"
	"monment/handler/request"
	"net/http"
)

func Publish(c *gin.Context) {
	var form request.Publish
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}
	var user model.User
	if err := user.IsUser(config.DB, form.UserId); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}
	post := model.Post{
		UserId:    form.UserId,
		Content:   form.Content,
		PostImage: form.PostImage,
		VisRange:  form.VisRange,
	}
	if err := post.Publish(config.DB); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "发布动态失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发布动态成功",
	})
	return
}
